package com.idbk.chargestation.activity.user;

import android.content.Context;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;

import com.android.volley.VolleyError;
import com.idbk.chargestation.R;
import com.idbk.chargestation.api.ChargeStationAPI;
import com.idbk.chargestation.base.BaseActivity;
import com.idbk.chargestation.bean.JsonLogin;
import com.idbk.chargestation.net.EHttpResponse;
import com.idbk.chargestation.util.GsonUtils;

/**
 * Created by wuz on 2019/1/23.
 */

public class ActivityBindPhone extends BaseActivity implements View.OnClickListener,
        OnEditorActionListener {

    private static final String TAG = ActivityBindPhone.class.getSimpleName();
    private Context mContext;
    private EditText mEdtPhone,mEdtPassword;
    private ImageView mImagePassword;
    private String mPassword,mPhone;
    private boolean mIsPasswordShowFlag = false;

    @Override
    protected void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_bindphone);

        mContext = this;

        initView();

    }

    public void initView(){
        initEditText();
        initOnClickView();
        initImageView();
    }

    public void initImageView(){
        mImagePassword = (ImageView) findViewById(R.id.bindphone_password_visible);
    }

    public void initEditText(){
        mEdtPassword =(EditText)findViewById(R.id.bindphone_password);
        mEdtPhone = (EditText)findViewById(R.id.bindphone_phone);
    }

    public void initOnClickView(){
        findViewById(R.id.bind).setOnClickListener(this);
        findViewById(R.id.toolbar_back).setOnClickListener(this);
        findViewById(R.id.bindphone_password_visible).setOnClickListener(this);
        mEdtPhone.setOnEditorActionListener(this);
        mEdtPassword.setOnEditorActionListener(this);
    }

    @Override
    public void onClick(View v){
        switch(v.getId()){
            case R.id.toolbar_back :
                finish();
                break;
            case R.id.bind ://绑定手机号
                gotobind();
                break;
            case R.id.bindphone_password_visible://密码可见状态切换
                setPasswordVisible();
                break;
            default:
                break;
        }
    }

    public void gotobind(){
        if(checkInput()){
            bind();
        }
    }

    private boolean checkInput(){
        mPassword = mEdtPassword.getText().toString();
        mPhone = mEdtPhone.getText().toString();
        if (mPhone.equals("") || (mPhone.length() != 11)){
            Toast.makeText(mContext, "请正确输入手机号码！", Toast.LENGTH_SHORT).show();
            mEdtPhone.setFocusable(true);
            return false;
        }
        if ( mPassword.length() != 0) {
            if (mPassword.length() < 6 || mPassword.length() > 20) {
                Toast.makeText(mContext, "请正确输入密码！", Toast.LENGTH_SHORT).show();
                mEdtPassword.setFocusable(true);
                return false;
            }
        }
        return true;

    }

    private void bind(){
        showMyProgressDialog(true,false,"绑定中...");
        mMainRequest = ChargeStationAPI.bindPhone(mPhone, mPassword, mResponse);
    }

    private final EHttpResponse mResponse = new EHttpResponse() {
        @Override
        public void onEResponse(String response) {
            JsonLogin result  = GsonUtils.toBean(JsonLogin.class,response);
            if (handleResponseStatus(result)){
                setResult(RESULT_OK);
//                Toast.makeText(mContext, "绑定成功", Toast.LENGTH_SHORT).show();
                showResult(mContext, "绑定成功!", true);
            }
        }

        @Override
        public void onEErrorResponse(VolleyError error) {
            Log.e(TAG, error.getMessage(), error.getCause());
            Toast.makeText(mContext, R.string.common_tip_resultfail, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onEFinish() {
            super.onEFinish();
            dismissMyProgressDialog();
        }
    };

    private void setPasswordVisible() {
        if (mIsPasswordShowFlag) {
            mIsPasswordShowFlag = false;
            mImagePassword.setImageResource(R.drawable.btn_login_password_hidden);
            mEdtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            mIsPasswordShowFlag = true;
            mImagePassword.setImageResource(R.drawable.btn_login_password_visible);
            mEdtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }


    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        return false;
    }
}
